﻿using System;
using System.Data;
using System.Collections;
using System.Data.SqlClient;
using System.Data.OracleClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Complus_DataAccess;
using System.Web;
using Ccit.Utils;
using System.Data.OleDb;

namespace Ccit.Utils
{
    /// <summary>
    /// 数据库连接信息类
    /// </summary>
    public class ConnectionInfo
    {
        /// <summary>
        /// 获取数据库连接字符串
        /// </summary>
        /// <returns></returns>
        public static string GetConnectionString()
        {
            DataAccessClass comDataAccess = new DataAccessClass();
            string sPduIdentify = HttpContext.Current.Application["PduIdentify"].ToString();
            string strConnection = comDataAccess.GetConnetionString(ConnectStringType.DotNet,sPduIdentify);      
            //string strConnection = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=F:\database\db3.mdb";
            //string strConnection = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=E:\tim\product\产品数据库\ACCESS\db3.mdb";
            Common.ReleaseComObject(comDataAccess);
            return strConnection;
        }

        /// <summary>
        /// 获取所连接的数据库类型
        /// </summary>
        /// <returns></returns>
        public static DataBaseType GetDataBaseType()
        {
            DataAccessClass comDataAccess = new DataAccessClass();
            string sPduIdentify = HttpContext.Current.Application["PduIdentify"].ToString();
            string sDbType = comDataAccess.getDataBaseType(sPduIdentify);
            //string sDbType = "4";
            Common.ReleaseComObject(comDataAccess);
            DataBaseType dbtReturn = (DataBaseType)Enum.Parse(typeof(DataBaseType), sDbType);
            return dbtReturn;
        }
    }

    /// <summary>
    /// 数据库类型枚举
    /// </summary>
    public enum DataBaseType
    {
        /// <summary>SQLServer数据库</summary>
        SQLServer = 1,
        /// <summary>Oracle数据库</summary>
        Oracle = 2,
        /// <summary>
        /// access数据库
        /// </summary>
        Access = 4
    }

    /// <summary>
    /// 数据字段类型枚举
    /// </summary>
    public enum DataType
    {
        /// <summary>
        /// 二进制
        /// </summary>
        Binary = 18,
        /// <summary>
        /// 日期
        /// </summary>
        Date = 7,
        /// <summary>
        /// 日期时间
        /// </summary>
        DateTime = 9,
        /// <summary>
        /// 数值
        /// </summary>
        Decimal = 3,
        /// <summary>
        /// 整数
        /// </summary>
        Int = 2,
        /// <summary>
        /// 文本
        /// </summary>
        NText = 4,
        /// <summary>
        /// 字符串
        /// </summary>
        String = 1,
        /// <summary>
        /// 时间
        /// </summary>
        Time = 8,
        /// <summary>
        /// 年月
        /// </summary>
        YearMonth = 17
    }

    /// <summary>
    /// 数据库操作类
    /// </summary>
    public class DataAccessor
    {
        /// <summary>
        ///		当前系统的连接串信息
        /// </summary>
        public static readonly string ConnectionString = ConnectionInfo.GetConnectionString();
        /// <summary>
        ///		当前系统的数据库类型
        /// </summary>
        public static readonly DataBaseType DataBaseType = ConnectionInfo.GetDataBaseType();

        #region 私有方法集合
        private static IDbCommand GetDbCommand()
        {
            IDbCommand cmd;
            if (DataAccessor.DataBaseType == DataBaseType.SQLServer)
            {
                cmd = new SqlCommand();
            }
            else if(DataAccessor.DataBaseType == DataBaseType.Oracle)
            {
                cmd = new OracleCommand();
            }
            else
            {
                cmd = new OleDbCommand();
            }

            return cmd;
        }

        /// <summary>
        /// 获取数据库连接对象
        /// </summary>
        /// <returns></returns>
        public static IDbConnection GetDbConnection()
        {
            IDbConnection conn;

            if (DataAccessor.DataBaseType == DataBaseType.SQLServer)
            {
                conn = new SqlConnection(DataAccessor.ConnectionString);
            }
            else if (DataAccessor.DataBaseType == DataBaseType.Oracle)
            {
                conn = new OracleConnection(DataAccessor.ConnectionString);
            }
            else
            {
                conn = new OleDbConnection(DataAccessor.ConnectionString);
            }

            return conn;
        }

        /// <summary>
        /// 获取数据库连接对象
        /// </summary>
        /// <param name="timeout">尝试执行SQL语句的等待时间</param>
        /// <returns></returns>
        public static IDbConnection GetDbConnection(int timeout)
        {
            IDbConnection conn;

            if (DataAccessor.DataBaseType == DataBaseType.SQLServer)
            {
                string strConn = DataAccessor.ConnectionString + ";Connection Timeout=" + timeout.ToString();
                conn = new SqlConnection(strConn);
            }
            else
            {
                conn = new OracleConnection(DataAccessor.ConnectionString);
            }

            return conn;
        }

        //public static void SetDbConnectionString(DataBaseType enDbType, string strConn)
        //{
        //    DataAccessor.DataBaseType = enDbType;
        //    DataAccessor.ConnectionString = strConn;
        //}

        private static IDataAdapter GetDataAdapter(IDbCommand selectCommand)
        {
            IDataAdapter adapter;
            if (DataAccessor.DataBaseType == DataBaseType.SQLServer)
            {
                adapter = new SqlDataAdapter((SqlCommand)selectCommand);
            }
            else if (DataAccessor.DataBaseType == DataBaseType.Oracle)
            {
                adapter = new OracleDataAdapter((OracleCommand)selectCommand);
            }
            else
            {
                adapter = new OleDbDataAdapter((OleDbCommand)selectCommand);
            }

            return adapter;
        }

        private static void PrepareCommand(IDbCommand cmd, IDbConnection conn, IDbTransaction trans, CommandType cmdType, string cmdText, IDataParameter[] cmdParams)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();

            cmd.Connection = conn;

            string replaceSign = string.Empty;

            if (DataAccessor.DataBaseType == DataBaseType.SQLServer || DataAccessor.DataBaseType.Equals(DataBaseType.Access))
            {
                replaceSign = "@";
            }
            else if (DataAccessor.DataBaseType == DataBaseType.Oracle)
            {
                replaceSign = ":";
            }
            cmdText = cmdText.Replace("#", replaceSign);

            cmd.CommandText = cmdText;

            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = cmdType;

            if (cmdParams != null)
            {
                foreach (IDataParameter param in cmdParams)
                    cmd.Parameters.Add(param);
            }
        }

        private static SqlDbType GetSqlDbTypeByDataType(DataType dataType)
        {
            switch (dataType)
            {
                case DataType.String:
                    {
                        return SqlDbType.NVarChar;
                    }
                case DataType.Int:
                    {
                        return SqlDbType.Int;
                    }
                case DataType.Decimal:
                    {
                        return SqlDbType.Decimal;
                    }
                case DataType.NText:
                    {
                        return SqlDbType.NText;
                    }
                case DataType.Date:
                case DataType.Time:
                case DataType.DateTime:
                case DataType.YearMonth:
                    {
                        return SqlDbType.Char;
                    }
                case DataType.Binary:
                    {
                        return SqlDbType.Binary;
                    }
            }
            return SqlDbType.NVarChar;
        }

        private static OracleType GetOracleTypeByDataType(DataType dataType)
        {
            switch (dataType)
            {
                case DataType.String:
                    {
                        return OracleType.NVarChar;
                    }
                case DataType.Int:
                    {
                        return OracleType.Number;
                    }
                case DataType.Decimal:
                    {
                        return OracleType.Float;
                    }
                case DataType.NText:
                    {
                        return OracleType.Clob;
                    }
                case DataType.Date:
                case DataType.Time:
                case DataType.DateTime:
                case DataType.YearMonth:
                    {
                        return OracleType.Char;
                    }
                case DataType.Binary:
                    {
                        return OracleType.Blob;
                    }
            }
            return OracleType.NVarChar;
        }


        private static OleDbType GetAccessTypeByDataType(DataType dataType)
        {
            switch (dataType)
            {
                case DataType.String:
                    {
                        return OleDbType.VarWChar;
                    }
                case DataType.Int:
                    {
                        return OleDbType.Integer;
                    }
                case DataType.Decimal:
                    {
                        return OleDbType.Numeric;
                    }
                case DataType.NText:
                    {
                        return OleDbType.LongVarWChar;
                    }
                case DataType.Date:
                case DataType.Time:
                case DataType.DateTime:
                case DataType.YearMonth:
                    {
                        return OleDbType.VarChar;
                    }
                case DataType.Binary:
                    {
                        return OleDbType.Binary;
                    }
            }
            return OleDbType.VarWChar;
        }

        private static IDataParameter GetDbParameter(string parameterName)
        {
            if (DataAccessor.DataBaseType == DataBaseType.SQLServer)
            {
                return new SqlParameter("@" + parameterName, null);
            }
            else if(DataAccessor.DataBaseType.Equals(DataBaseType.Oracle))
                return new OracleParameter(":" + parameterName, null);
            else
                return new OleDbParameter("@" + parameterName, null);
        }

        private static int GetDataLengthByDataType(DataType dataType)
        {
            int num1 = -1;
            switch (dataType)
            {
                case DataType.String:
                    {
                        return -1;
                    }
                case DataType.Int:
                case DataType.NText:
                case DataType.Binary:
                    {
                        return -1;
                    }
                case DataType.Decimal:
                    {
                        return -1;
                    }
                case (DataType.NText | DataType.String):
                case (DataType.NText | DataType.Int):
                    {
                        return num1;
                    }
                case DataType.Date:
                    {
                        return 10;
                    }
                case DataType.Time:
                    {
                        return 8;
                    }
                case DataType.DateTime:
                    {
                        return 20;
                    }
                case DataType.YearMonth:
                    {
                        return 7;
                    }
            }
            return num1;
        }


        #endregion 私有方法集合

        /// <summary>
        /// 获取数据字段参数对象
        /// </summary>
        /// <param name="parameterName">数据字段参数名称</param>
        /// <param name="dataType">数据字段参数类型</param>
        /// <returns></returns>
        public static IDataParameter GetDataParameter(string parameterName, DataType dataType)
        {
            IDataParameter parameter1 = DataAccessor.GetDbParameter(parameterName);
            int num1 = DataAccessor.GetDataLengthByDataType(dataType);
            if (DataAccessor.DataBaseType == DataBaseType.SQLServer)
            {
                ((SqlParameter)parameter1).SqlDbType = DataAccessor.GetSqlDbTypeByDataType(dataType);
                if (num1 > -1)
                {
                    ((SqlParameter)parameter1).Size = num1;
                }
            }
            else if (DataAccessor.DataBaseType.Equals(DataBaseType.Oracle))
            {
                ((OracleParameter)parameter1).OracleType = DataAccessor.GetOracleTypeByDataType(dataType);
                if (num1 > -1)
                {
                    ((OracleParameter)parameter1).Size = num1;
                }
            }
            else
            {
                ((OleDbParameter)parameter1).OleDbType = DataAccessor.GetAccessTypeByDataType(dataType);
                if (num1 > -1)
                {
                    ((OleDbParameter)parameter1).Size = num1;
                }
            }
            return parameter1;
        }

        /// <summary>
        /// 过去数据字段参数对象
        /// </summary>
        /// <param name="parameterName">数据字段参数名称</param>
        /// <param name="dataType">数据字段参数类型</param>
        /// <param name="parameterSize">参数大小</param>
        /// <returns></returns>
        public static IDataParameter GetDataParameter(string parameterName, DataType dataType, int parameterSize)
        {
            IDataParameter parameter1 = DataAccessor.GetDbParameter(parameterName);
            int num1 = DataAccessor.GetDataLengthByDataType(dataType);
            if (DataAccessor.DataBaseType == DataBaseType.SQLServer)
            {
                ((SqlParameter)parameter1).SqlDbType = DataAccessor.GetSqlDbTypeByDataType(dataType);
                if (num1 == -1)
                {
                    num1 = parameterSize;
                }
                ((SqlParameter)parameter1).Size = num1;
            }
            else if (DataAccessor.DataBaseType.Equals(DataBaseType.Oracle))
            {
                ((OracleParameter)parameter1).OracleType = DataAccessor.GetOracleTypeByDataType(dataType);
                if (num1 == -1)
                {
                    num1 = parameterSize;
                }
                ((OracleParameter)parameter1).Size = num1;
            }
            else {
                ((OleDbParameter)parameter1).OleDbType = DataAccessor.GetAccessTypeByDataType(dataType);
                if (num1 == -1)
                {
                    num1 = parameterSize;
                }
                ((OleDbParameter)parameter1).Size = num1;                
            }


            return parameter1;
        }

        /// <summary>
        ///		执行SQL语句,并返回受影响的行数
        ///		对于DML语句中的INSERT,UPDATE,DELETE语句,返回该命令受影响的行数,对于其它所有DML语句,返回-1
        ///		对于DDL语句,比如"CREATE TABLE"或者"ALTER TABLE",返回0
        /// </summary>
        /// <param name="cmdType">CommandType枚举值,表示cmdText的类型</param>
        /// <param name="cmdText">执行的SQL语句</param>
        /// <returns></returns>
        public static int ExecuteNonQuery(CommandType cmdType, string cmdText)
        {
            IDbCommand cmd = GetDbCommand();

            //try
            //{
                using (IDbConnection conn = GetDbConnection())
                {
                    PrepareCommand(cmd, conn, null, cmdType, cmdText, null);
                    int iReturnValue = cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                    return iReturnValue;
                }
            //}
            //catch
            //{
            //    throw;
            //}
        }

        /// <summary>
        ///		执行SQL语句,并返回受影响的行数
        ///		对于DML语句中的INSERT,UPDATE,DELETE语句,返回该命令受影响的行数,对于其它所有DML语句,返回-1
        ///		对于DDL语句,比如"CREATE TABLE"或者"ALTER TABLE",返回0
        /// </summary>
        /// <param name="cmdText">执行的SQL语句</param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string cmdText)
        {
            return ExecuteNonQuery(CommandType.Text, cmdText);
        }

        /// <summary>
        ///		执行SQL语句,并返回受影响的行数
        ///		对于DML语句中的INSERT,UPDATE,DELETE语句,返回该命令受影响的行数,对于其它所有DML语句,返回-1
        ///		对于DDL语句,比如"CREATE TABLE"或者"ALTER TABLE",返回0
        /// </summary>
        /// <param name="cmdType">CommandType枚举值,表示cmdText的类型</param>
        /// <param name="cmdText">执行的SQL语句</param>
        /// <param name="parameters">执行的SQL语句的参数</param>
        /// <returns></returns>
        public static int ExecuteNonQuery(CommandType cmdType, string cmdText, IDataParameter[] parameters)
        {
            IDbCommand cmd = GetDbCommand();

            using (IDbConnection conn = GetDbConnection())
            {
                try
                {
                    PrepareCommand(cmd, conn, null, cmdType, cmdText, parameters);
                    int iReturnValue = cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                    return iReturnValue;
                }
                catch
                {
                    throw;
                }
            }
        }

        /// <summary>
        ///		执行SQL语句,并返回受影响的行数
        ///		对于DML语句中的INSERT,UPDATE,DELETE语句,返回该命令受影响的行数,对于其它所有DML语句,返回-1
        ///		对于DDL语句,比如"CREATE TABLE"或者"ALTER TABLE",返回0 
        /// </summary>
        /// <param name="cmdText">执行的SQL语句</param>
        /// <param name="parameters">执行的SQL语句的参数</param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string cmdText, IDataParameter[] parameters)
        {
            return ExecuteNonQuery(CommandType.Text, cmdText, parameters);
        }

        /// <summary>
        ///		执行SQL语句,并返回受影响的行数,此方法通过事务提交所有执行的SQL语句,如没有特殊要求,可以使用该方法
        ///		的重载函数(以string[]或者ArrayList为参数的重载函数)来执行带事务的多个SQL语句
        ///		对于DML语句中的INSERT,UPDATE,DELETE语句,返回该命令受影响的行数,对于其它所有DML语句,返回-1
        ///		对于DDL语句,比如"CREATE TABLE"或者"ALTER TABLE",返回0
        /// </summary>
        /// <param name="trans">表示要在数据库中处理的Transaction-SQL事务</param>
        /// <param name="cmdType">CommandType枚举值,表示cmdText的类型</param>
        /// <param name="cmdText">执行的SQL语句</param>
        /// <returns></returns>
        public static int ExecuteNonQuery(IDbTransaction trans, CommandType cmdType, string cmdText)
        {
            IDbCommand cmd = GetDbCommand();

            try
            {
                PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, null);
                int iReturnValue = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                return iReturnValue;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        ///		执行类型是CommandType.Text的SQL语句,并返回受影响的行数,此方法通过事务提交所有执行的SQL语句
        ///		对于DML语句中的INSERT,UPDATE,DELETE语句,返回该命令受影响的行数,对于其它所有DML语句,返回-1
        ///		对于DDL语句,比如"CREATE TABLE"或者"ALTER TABLE",返回0
        /// </summary>
        /// <param name="arrCmdText">多个需要同时执行的SQL语句字符串数组</param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string[] arrCmdText)
        {
            int iReturnValue = 1;
            using (IDbConnection conn = GetDbConnection())
            {
                conn.Open();
                using (IDbTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        int iCmdLength = arrCmdText.Length;
                        for (int i = 0; i < iCmdLength; ++i)
                        {
                            string sCmdText = arrCmdText[i];
                            ExecuteNonQuery(trans, CommandType.Text, sCmdText);
                        }
                        trans.Commit();
                    }
                    catch
                    {
                        iReturnValue = -1;
                        trans.Rollback();
                        throw;
                    }
                }
            }
            return iReturnValue;
        }

        #region logout code tim
        
        //public static int ExecuteNonQuery(ArrayList cmdTextList, ArrayList paramsList, ArrayList indexList, bool throwException)
        //{
        //    int iReturnValue = 1;
        //    using (IDbConnection conn = GetDbConnection())
        //    {
        //        conn.Open();
        //        using (IDbTransaction trans = conn.BeginTransaction())
        //        {
        //            try
        //            {
        //                int iCmdLength = cmdTextList.Count;
        //                for (int i = 0; i < iCmdLength; ++i)
        //                {
        //                    int paramsIndex = indexList.BinarySearch(i);
        //                    string cmdText = cmdTextList[i].ToString();
        //                    if (paramsIndex > -1)
        //                    {
        //                        // 如果indexList中能够找到i，那么需要使用带参数方式执行Command
        //                        IDbCommand cmd = GetDbCommand();

        //                        PrepareCommand(cmd, trans.Connection, trans, CommandType.Text, cmdText, (IDataParameter[])paramsList[paramsIndex]);
        //                        cmd.ExecuteNonQuery();
        //                        cmd.Parameters.Clear();
        //                    }
        //                    else
        //                    {
        //                        ExecuteNonQuery(trans, CommandType.Text, cmdText);
        //                    }

        //                }
        //                trans.Commit();
        //            }
        //            catch
        //            {
        //                iReturnValue = -1;
        //                trans.Rollback();
        //                if (throwException)
        //                    throw;
        //            }
        //        }
        //    }
        //    return iReturnValue;
        //}
        #endregion

        /// <summary>
        ///		执行类型是CommandType.Text的SQL语句,并返回受影响的行数,
        ///		此方法通过事务提交所有执行的SQL语句,该方法用于SQL语句的数量是动态生成的情况
        ///		对于DML语句中的INSERT,UPDATE,DELETE语句,返回该命令受影响的行数,对于其它所有DML语句,返回-1
        ///		对于DDL语句,比如"CREATE TABLE"或者"ALTER TABLE",返回0
        /// </summary>
        /// <param name="arrayListCmdText">多个SQL语句组成的ArrayList对象</param>
        /// <returns></returns>
        public static int ExecuteNonQuery(ArrayList arrayListCmdText)
        {
            int iCmdCount = arrayListCmdText.Count;
            string[] sArrCmdText = new string[iCmdCount];
            for (int i = 0; i < iCmdCount; ++i)
            {
                sArrCmdText[i] = arrayListCmdText[i].ToString();
            }

            try
            {
                int iexe = ExecuteNonQuery(sArrCmdText);
                return iexe;
            }
            catch
            {
                throw;
            }            
        }


        /// <summary>
        ///		执行类型是CommandType.Text的SQL语句,并返回受影响的行数,
        ///		此方法通过事务提交所有执行的SQL语句,该方法用于SQL语句的数量是动态生成的情况
        ///		对于DML语句中的INSERT,UPDATE,DELETE语句,返回该命令受影响的行数,对于其它所有DML语句,返回-1
        ///		对于DDL语句,比如"CREATE TABLE"或者"ALTER TABLE",返回0
        /// </summary>
        /// <param name="lstCmdText">SQL语句集合</param>
        /// <returns></returns>
        public static int ExecuteNonQuery(List<string> lstCmdText)
        {
            int iCmdCount = lstCmdText.Count;
            string[] sArrCmdText = new string[iCmdCount];
            for (int i = 0; i < iCmdCount; ++i)
            {
                sArrCmdText[i] = lstCmdText[i].ToString();
            }

            try
            {
                return ExecuteNonQuery(sArrCmdText);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        ///		执行cmdText,并返回一个DataSet对象
        /// </summary>
        /// <param name="cmdType">CommandType枚举值,表示cmdText的类型</param>
        /// <param name="cmdText">执行的SQL语句</param>
        /// <returns>DataSet结果集</returns>
        public static DataSet ExecuteDataSet(CommandType cmdType, string cmdText)
        {
            IDbCommand cmd = GetDbCommand();
            IDbConnection conn = GetDbConnection();
            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, null);
                IDataAdapter adapter = GetDataAdapter(cmd);
                DataSet dsReturn = new DataSet();
                adapter.Fill(dsReturn);
                return dsReturn;
            }
            catch
            {
                throw;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }

        /// <summary>
        ///		执行类型是CommandType.Text的cmdText进行查询,并返回一个DataSet对象
        /// </summary>
        /// <param name="cmdText">执行的SQL语句</param>
        /// <returns>DataSet结果集</returns>
        public static DataSet ExecuteDataSet(string cmdText)
        {
            return ExecuteDataSet(CommandType.Text, cmdText);
        }

        /// <summary>
        /// 执行类型是CommandType.Text的cmdText进行查询,并返回一个DataSet对象
        /// </summary>
        /// <param name="cmdText">执行的SQL语句</param>
        /// <param name="connTimeout">尝试建立数据库连接等待时间</param>
        /// <param name="cmdTimeout">尝试执行SQL语句等待时间</param>
        /// <returns>DataSet结果集</returns>
        public static DataSet ExecuteDataSet(string cmdText, int connTimeout,int cmdTimeout)
        {
            IDbCommand cmd = GetDbCommand();
            IDbConnection conn = GetDbConnection(connTimeout);
            
            try
            {
                cmd.CommandTimeout = cmdTimeout;
                PrepareCommand(cmd, conn, null, CommandType.Text, cmdText, null);
                IDataAdapter adapter = GetDataAdapter(cmd);
                DataSet dsReturn = new DataSet();
                adapter.Fill(dsReturn);
                return dsReturn;
            }
            catch
            {
                throw;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }

        /// <summary>
        ///		执行类型是CommandType.Text的cmdText进行查询,并根据当前页码以及结果集每页包含的记录数返回符合条件的
        ///		DataSet对象,同时以out参数方式返回所有记录的数量
        /// </summary>
        /// <param name="cmdText">执行的类型是CommandType.Text的SQL语句</param>
        /// <param name="currentPageIndex">需要返回的结果集的页码</param>
        /// <param name="pageSize">结果集每页包含的记录数</param>
        /// <param name="totalRecordCount">所有记录数</param>
        /// <returns>DataSet结果集</returns>
        public static DataSet ExecuteDataSet(string cmdText, int currentPageIndex, int pageSize, out int totalRecordCount)
        {
            int iPageSize = pageSize;
            int iCurrentPageIndex = currentPageIndex;
            int iRowCount = 0;
            DataSet dsReturn = new DataSet();

            DataSet dsOriginal = DataAccessor.ExecuteDataSet(cmdText);
            if (dsOriginal.Tables.Count > 0)
            {
                DataTable dTable = dsOriginal.Tables[0];
                iRowCount = dTable.Rows.Count;
                int iColCount = dTable.Columns.Count;

                int iStartRecordIndex = iPageSize * iCurrentPageIndex;
                int iEndRecordIndex = iPageSize * (iCurrentPageIndex + 1);
                if (iEndRecordIndex > iRowCount)
                    iEndRecordIndex = iRowCount;

                DataTable dtCopy = new DataTable();
                for (int i = 0; i < iColCount; ++i)
                {
                    dtCopy.Columns.Add(dTable.Columns[i].ColumnName, typeof(string));
                }

                for (int i = iStartRecordIndex; i < iEndRecordIndex; ++i)
                {
                    DataRow dr = dtCopy.NewRow();
                    for (int j = 0; j < iColCount; ++j)
                    {
                        string sColName = dTable.Columns[j].ColumnName;
                        dr[sColName] = dTable.Rows[i][j].ToString();
                    }
                    dtCopy.Rows.Add(dr);
                }
                dsReturn.Tables.Add(dtCopy);
            }
            totalRecordCount = iRowCount;
            return dsReturn;
        }
             


        /// <summary>
        ///		执行查询,并返回查询所返回的结果集中第一行的第一列,忽略额外的行或列
        ///		使用此方法从数据源中检索一个单个值(比如Count或者Max这样的聚合值),速度比普通方法快很多
        /// </summary>
        /// <param name="cmdType">CommandType枚举值,表示cmdText的类型</param>
        /// <param name="cmdText">执行的SQL语句</param>
        /// <returns></returns>
        public static object ExecuteScalar(CommandType cmdType, string cmdText)
        {
            IDbCommand cmd = GetDbCommand();

            using (IDbConnection conn = GetDbConnection())
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, null);
                object val = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                return val;
            }
        }

        /// <summary>
        /// 执行查询，并返回Reader对象
        /// </summary>
        /// <param name="conn">数据库连接对象</param>
        /// <param name="cmdText">SQL 语句</param>
        /// <returns>IDataReader 对象</returns>
        public static IDataReader ExecuteReader(IDbConnection conn, string cmdText)
        {
            IDbCommand cmd = GetDbCommand();

            PrepareCommand(cmd, conn, null, CommandType.Text, cmdText, null);
            IDataReader dataReader = cmd.ExecuteReader();
            return dataReader;
        }

        /// <summary>
        /// 执行查询，并返回Reader对象
        /// </summary>
        /// <param name="cmdText">SQL 语句</param>
        /// <returns>IDataReader 对象</returns>
        public static IDataReader ExecuteReader(string cmdText)
        {
            IDbCommand cmd = GetDbCommand();
            IDbConnection conn = GetDbConnection();
            try
            {
                PrepareCommand(cmd, conn, null, CommandType.Text, cmdText, null);
                IDataReader dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return dataReader;
            }
            catch
            {
                conn.Close();
                throw;
            }
        }

        /// <summary>
        ///		执行类型是CommandType.Text的cmdText进行查询,并返回查询所返回的结果集中第一行的第一列,
        ///		忽略额外的行或列,使用此方法从数据源中检索一个单个值(比如Count或者Max这样的聚合值),速度比普通方法快很多
        /// </summary>
        /// <param name="cmdText">执行的SQL语句</param>
        /// <returns></returns>
        public static object ExecuteScalar(string cmdText)
        {
            return ExecuteScalar(CommandType.Text, cmdText);
        }
    }
}
